home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1996
/
MacHack 1996.toast
/
Presentations
/
Presentations ’92
/
PatchWorks Kit
/
<PatchWorks++>
/
TestExtension.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-21
|
5KB
|
237 lines
/*
TestExtension.c
TestBed for developing extensions.
Pretends to be an INIT and calls Install().
by Mouse Herrell & Patrick Beard.
© 1991 Berkeley Systems Inc.
*/
#include <Windows.h>
#include <GestaltEqu.h>
#include <Notification.h>
#include <stdio.h>
#include <stdlib.h>
#include "ScalperPatch.h"
#include "TestExtension.h"
#include "Exceptions.h"
enum {
eAppleID = 1,
eFileID,
eEditID
};
enum {
eQuitItem = 1
};
void ExitTask(void);
void HandleMenu(long mSelect);
void ReportError(OSErr err, char* stage);
static MenuHandle theAppleMenu, theFileMenu, theEditMenu;
static short theFamily = -1;
static Boolean theInstalledFlag = false;
short theSystemVersion;
Boolean theCQDFlag;
OSErr main(void)
{
OSErr err;
static char* stage;
SysEnvRec env;
SysEnvirons(1, &env);
theSystemVersion = env.systemVersion;
theCQDFlag = env.hasColorQD;
MaxApplZone();
try {
long pval;
stage = "installing the cleanup task";
err = _atexit(ExitTask);
if (err)
throw (err);
stage = "requesting the Scalper hook";
ScalperPatch::InstallScalper();
InitGraf(&thePort);
stage = "calling Install()";
theInstalledFlag = true;
Install();
} catch {
err = theException;
ReportError(err, stage);
}
try {
BitMap map = { 0, 4, { 16, 80, 48, 112 } };
stage = "initializing the Toolbox";
InitGraf(&thePort);
InitCursor();
InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(0);
FlushEvents(everyEvent, 0);
stage = "creating the menus";
InsertMenu(theAppleMenu = NewMenu(eAppleID, "\p\024"), 0);
InsertMenu(theFileMenu = NewMenu(eFileID, "\pFile"), 0);
InsertMenu(theEditMenu = NewMenu(eEditID, "\pEdit"), 0);
DrawMenuBar();
AddResMenu(theAppleMenu, 'DRVR');
AppendMenu(theFileMenu, "\pQuit/Q");
AppendMenu(theEditMenu, "\pUndo/Z;(-;Cut/X;Copy/C;Paste/V;Clear");
if (theFamily != -1) {
WindowPtr window = 0;
Rect r = { 64, 64, 128, 256 };
Handle icon = GetResource('ICN#', theFamily);
stage = "loading the icon";
if (!icon) {
OSErr err = ResError();
if (err == noErr)
err = memFullErr;
throw (err);
}
HLock(icon);
map.baseAddr = *icon;
stage = "allocating the window record";
window = NewWindow(NULL, &r, CurApName, true, movableDBoxProc, NULL, false, 0);
if (!window)
throw (memFullErr);
SetPort(window);
}
stage = "in the event loop";
while (1) {
EventRecord theEvent;
if (WaitNextEvent(everyEvent, &theEvent, 0, nil)) {
switch (theEvent.what) {
WindowPtr theWindow;
int windowCode;
case mouseDown:
windowCode = FindWindow(theEvent.where, &theWindow);
switch (windowCode) {
Rect dragRect;
case inSysWindow:
SystemClick(&theEvent, theWindow);
break;
case inMenuBar:
HandleMenu(MenuSelect(theEvent.where));
break;
case inDrag:
dragRect = screenBits.bounds;
dragRect.top = GetMBarHeight();
InsetRect(&dragRect, 4, 4);
DragWindow(theWindow, theEvent.where, &dragRect);
break;
case inContent:
if (theWindow != FrontWindow())
SelectWindow(theWindow);
break;
case inGoAway:
if (TrackGoAway(theWindow, theEvent.where))
ExitToShell();
break;
}
break;
case keyDown:
case autoKey:
if ((theEvent.modifiers & cmdKey) != 0)
HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
break;
case updateEvt:
theWindow = (WindowPtr)theEvent.message;
BeginUpdate(theWindow);
if (theFamily != -1) {
CopyBits(&map, &theWindow->portBits, &map.bounds, &map.bounds,
srcCopy, theWindow->visRgn);
}
EndUpdate(theWindow);
break;
case activateEvt:
break;
}
HiliteMenu(0);
}
}
} catch {
static char message[256];
err = theException;
sprintf(message, "An error of type %hd occurred while %s.", err, stage);
CtoPstr(message);
DebugStr(message);
}
}
void ReportError(OSErr err, char* stage)
{
static char message[256];
static NMRec notification = { 0, nmType };
notification.nmMark = 0;
notification.nmIcon = 0;
notification.nmSound = (void*)-1;
sprintf(message, "An error of type %hd occurred while %s.", err, stage);
notification.nmStr = c2pstr(message);
notification.nmResp = (void*)-1;
NMInstall(¬ification);
}
void HandleMenu(long mSelect)
{
int menuID = HiWord(mSelect);
int menuItem = LoWord(mSelect);
Str255 name;
GrafPtr savePort;
WindowPeek frontWindow;
switch (menuID) {
case eAppleID:
GetPort(&savePort);
GetItem(theAppleMenu, menuItem, name);
OpenDeskAcc(name);
SetPort(savePort);
break;
case eFileID:
if (menuItem == eQuitItem)
ExitToShell();
break;
case eEditID:
if (!SystemEdit(menuItem-1))
SysBeep(5);
}
}
void ExitTask(void)
{
if (theInstalledFlag)
Remove();
ScalperPatch::RemoveScalper();
}
void ShowIconFamily(short iconId)
{
theFamily = iconId;
}